package com.dylan.历史.领扣;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : ankang
 * @date : 2019/10/10 21:20
 * @Description :
 */
public class $0078子集 {

    public static void main(String[] args) {
        List<List<Integer>> a = new Solution().subsets(new int[]{1, 2, 3, 4});
        System.out.println(a);
    }

    static class Solution {
        public List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> list = new ArrayList<>();
            if (nums == null || nums.length == 0) {
                list.add(new ArrayList<>());
                return list;
            }
            boolean[] indexes = new boolean[nums.length];
            subsets(list, nums, indexes, 0);
            return list;
        }

        /**
         *
         * @param list 结果
         * @param nums 全集
         * @param indexes 与全集数量相等的数组，标识对应元素是否选中
         * @param index 待考察元素的下标
         */
        private void subsets(List<List<Integer>> list, int[] nums, boolean[] indexes, int index) {
            if (index == indexes.length - 1) {
                indexes[index] = false;
                list.add(subsets(nums, indexes));
                indexes[index] = true;
                list.add(subsets(nums, indexes));
            } else {
                indexes[index] = false;
                subsets(list, nums, indexes, index + 1);
                indexes[index] = true;
                subsets(list, nums, indexes, index + 1);
            }
        }

        private List<Integer> subsets(int[] nums, boolean[] indexes) {
            List<Integer> subList = new ArrayList<>();
            for (int i = 0; i < indexes.length; i++) {
                if (indexes[i]) {
                    subList.add(nums[i]);
                }
            }
            return subList;
        }
    }
}
